package com.xq.tmall.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.xq.tmall.controller.BaseController;
import com.xq.tmall.entity.Notice;
import com.xq.tmall.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping(value = "/admin/notice", produces = "application/json;charset=utf-8")
public class AdminNoticeController extends BaseController {

    @Autowired
    private NoticeService noticeService;

    //添加公告信息-ajax
    @ResponseBody
    @PostMapping(value = "/addNotice")
    public String addCategory( Notice notice) {
        JSONObject jsonObject = new JSONObject();
        Integer id = noticeService.insert(notice);
        if (id>0) {
            logger.info("添加成功！,新增公告的ID值为：{}", id);
            jsonObject.put("success", true);
            jsonObject.put("category_id", id);
        } else {
            jsonObject.put("success", false);
            logger.warn("添加失败！事务回滚");
            throw new RuntimeException();
        }
        return jsonObject.toJSONString();
    }


    @ResponseBody
    @PostMapping(value = "/updateNotice")
    public String updateNotice( Notice notice) {
        JSONObject jsonObject = new JSONObject();
        Boolean update = noticeService.update(notice);
        if (update) {
            logger.info("修改成功！");
            jsonObject.put("success", true);
        } else {
            jsonObject.put("success", false);
            logger.warn("修改失败！事务回滚");
            throw new RuntimeException();
        }
        return jsonObject.toJSONString();
    }

}
